package com.ning.cloud.framework.security.feign;

import com.alibaba.fastjson2.JSONObject;
import com.ning.cloud.common.constant.CommonConstant;
import com.ning.cloud.framework.security.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * feign拦截器，传递用户信息
 */
@Slf4j
public class FeignUserInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate requestTemplate) {
		String userStr = URLEncoder.encode(JSONObject.toJSONString(UserContext.getAuthUser()), StandardCharsets.UTF_8);
		requestTemplate.header(CommonConstant.HEADER_LOGIN_USER, userStr);
	}

}
